<cfsetting enablecfoutputonly="yes">

<cfparam name="attributes.url" default="#application.xtiurl#">
<cfparam name="attributes.dtd" default="#application.xtidtd#">
<cfparam name="attributes.debug" default="false">
<cfparam name="attributes.return_false" default="0">
<cfparam name="attributes.debuglevel" default="1,2,3">
<cfparam name="attributes.customerid" default="">
<cfparam name="attributes.contactid" default="">
<cfparam name="attributes.type">
<!--- NAME INFO --->
<cfparam name="attributes.prefix" default="">
<cfparam name="attributes.first_name">
<cfparam name="attributes.midinit" default="">
<cfparam name="attributes.last_name">
<cfparam name="attributes.suffix" default="">
<cfparam name="attributes.abinum" default="">
<cfparam name="attributes.company_department">
<cfparam name="attributes.company_division" default="">
<!--- BILLING INFO --->
<cfparam name="attributes.b_company_name" default="">
<cfparam name="attributes.b_address1">
<cfparam name="attributes.b_address2" default="">
<cfparam name="attributes.b_city">
<cfparam name="attributes.b_state" default="">
<cfparam name="attributes.b_zip" default="">
<cfparam name="attributes.b_geo_code" default="">
<cfparam name="attributes.b_country">
<cfparam name="attributes.b_phone">
<cfparam name="attributes.b_ext" default="">
<!--- LITERATURE --->
<cfparam name="attributes.lit_add_array" default="">
<cfparam name="attributes.lit_delete_array" default="">
<!--- OTHER --->
<cfparam name="attributes.alt_phone" default="">
<cfparam name="attributes.alt_ext" default="">
<cfparam name="attributes.fax_phone" default="">
<cfparam name="attributes.email">
<cfparam name="attributes.web_response">
<cfparam name="attributes.web_challenge">
<cfparam name="attributes.flag_esf" default="Y">
<cfparam name="attributes.flag_spf" default="Y">
<cfparam name="attributes.source" default="web">
<cfparam name="attributes.user" default="web">
<cfparam name="attributes.pay_type" default="">
<cfparam name="attributes.account_active" default="">
<cfparam name="attributes.tax_resale_number" default="">
<cfparam name="attributes.tsr_own" default="">
<cfparam name="attributes.tsr_take" default="">
<cfparam name="attributes.avs_at_contact" default="">
<cfparam  name="attributes.ds_loc"  default="">
<cfparam  name="attributes.auto_catalog"  default="">

<cfset attributes.b_company_name=replace(attributes.b_company_name, "&", "and","all")>
<cfset attributes.b_address1=replace(attributes.b_address1, "&", "and", "all")>
<cfset attributes.b_address2=replace(attributes.b_address2, "&", "and", "all")>
<cfset attributes.email=replace(attributes.email, ";", "", "all")>


<!--- Tag Constraints --->
<!--- <cfif not ListContainsNocase('H,A,B,B1,B2,B3,B4,B5,B6,B7,B8,B9,BA,BB,C,D,E,E1,E2,E3,E4,G', attributes.type)>
	<cfoutput>Invalid "TYPE" field in tag xti_createcustomer.</cfoutput><cf_page_abort>
</cfif> --->
<cfif attributes.return_false>
	<cfset temp = setvariable('caller.xti_createcustomer.result', false)>
	<cfsetting enablecfoutputonly="no">
	<cfexit method="exittag">
</cfif>

<cfif listfindnocase("H,HE",attributes.type)>
	<!--- Cannot Create contact for home user --->
	<cfthrow type="Custom" message="Cannot create a contact for home accounts.">
<cfelse>
	<cfset calltype = "crt_contact">
</cfif>

<!--- Create XML for LIT_PAK tags - parse out the array
action A = add, action D = delete.
does customer want to recive catalogs? --->
<cfparam name="lit_pak_add" default="">
<cfparam name="lit_pak_delete" default="">

<cfif isarray(attributes.lit_add_array)>
	<cfscript>
	lit_pak_add = "";
	indexcnt = arraylen(attributes.lit_add_array) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
	     lit_pak_add = lit_pak_add & '         
	 <lit_pak>
	   <lit_name>#ucase(attributes.lit_add_array[i].lit_name)#</lit_name>
	   <lit_action>A</lit_action>
	 </lit_pak>';
	}
	</cfscript>
</cfif>

<cfif isarray(attributes.lit_delete_array)>
	<cfscript>
	lit_pak_delete = "";
	indexcnt = arraylen(attributes.lit_delete_array) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
	     lit_pak_delete = lit_pak_delete & '         
	 <lit_pak>
	   <lit_name>#ucase(attributes.lit_delete_array[i].lit_name)#</lit_name>
	   <lit_action>D</lit_action>
	 </lit_pak>';
	}
	</cfscript>
</cfif>

<!--- Build the xml to send to middleware--->
<cfscript>
xml = 
'<!DOCTYPE copy SYSTEM "#attributes.dtd#">
<xtiinput>
   <inputheader>
      <actionset>
         <action>#calltype#</action>
      </actionset>
   </inputheader>
   <inputdata>
      <customer>
         <customerid>#ucase(attributes.customerid)#</customerid>
         <contactid>#ucase(attributes.contactid)#</contactid>
         <type>#ucase(attributes.type)#</type>
         <prefix>#ucase(attributes.prefix)#</prefix>
         <first_name>#ucase(rereplace(attributes.first_name,"&","AND","ALL"))#</first_name>
         <midinit>#ucase(attributes.midinit)#</midinit>
         <last_name>#ucase(rereplace(attributes.last_name,"&","AND","ALL"))#</last_name>
         <suffix>#ucase(attributes.suffix)#</suffix>
         <abinum>#ucase(attributes.abinum)#</abinum>
         <company_department>#ucase(rereplace(attributes.company_department,"&","AND","ALL"))#</company_department>
         <company_division>#ucase(rereplace(attributes.company_division,"&","AND","ALL"))#</company_division>
         <billing>
            <company_name>#ucase(rereplace(attributes.b_company_name,"&","AND","ALL"))#</company_name>
            <address_1>#ucase(rereplace(attributes.b_address1,"&","AND","ALL"))#</address_1>
            <address_2>#ucase(rereplace(attributes.b_address2,"&","AND","ALL"))#</address_2>
            <city>#ucase(rereplace(attributes.b_city,"&","AND","ALL"))#</city>
            <state>#ucase(rereplace(attributes.b_state,"&","AND","ALL"))#</state>
            <zip>#ucase(attributes.b_zip)#</zip>
            <geo_code>#ucase(attributes.b_geo_code)#</geo_code>
            <country>#ucase(attributes.b_country)#</country>
            <phone>#rereplace(attributes.b_phone, '[^0-9]', '', 'all')#</phone>
         </billing>
		 #lit_pak_add#
		 #lit_pak_delete#
         <ext>#ucase(attributes.b_ext)#</ext>
         <alt_phone>#rereplace(attributes.alt_phone, '[^0-9]', '', 'all')#</alt_phone>
         <alt_ext>#ucase(attributes.alt_ext)#</alt_ext>
         <fax_phone>#rereplace(attributes.fax_phone, '[^0-9]', '', 'all')#</fax_phone>
         <email>#attributes.email#</email>
         <web_response>#ucase(attributes.web_response)#</web_response>
         <web_challenge>#ucase(attributes.web_challenge)#</web_challenge>
         <esf>#ucase(attributes.flag_esf)#</esf>
         <spf>#ucase(attributes.flag_spf)#</spf>
         <source>#ucase(attributes.source)#</source>
         <user>#ucase(attributes.user)#</user>
         <pay_type>#ucase(attributes.pay_type)#</pay_type>
         <account_active>#ucase(attributes.account_active)#</account_active>
         <tax_resale_number>#ucase(attributes.tax_resale_number)#</tax_resale_number>
         <tsr_own>#ucase(attributes.tsr_own)#</tsr_own>
         <tsr_take>#ucase(attributes.tsr_take)#</tsr_take>
         <avs_at_contact>#ucase(attributes.avs_at_contact)#</avs_at_contact>
		 <ds_loc>#attributes.ds_loc#</ds_loc>
		 <auto_catalog>#attributes.auto_catalog#</auto_catalog>
      </customer>
   </inputdata>
</xtiinput>';
</cfscript>